package com.example.petstore.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.petstore.common.CommonResponse;
import com.example.petstore.service.CategoryService;
import com.example.petstore.service.GiteeService;
import com.example.petstore.service.ProductService;
import com.example.petstore.service.UserService;
import com.example.petstore.util.GiteeLogin;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

//@SessionAttributes(value = {"giteeCustome", "giteeReason", "loginUser", "giteeId", "verifyCode"})
@Controller
@SessionAttributes({"loginAdmin"})
public class CommonController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private GiteeService giteeService;

    @GetMapping("/login")
    public String login(){
//        todo: 到跳转页面
        return "test_index";
    }

    @GetMapping("/index")
    public String index(Model model){
        List<String> categoryNameList = categoryService.getCategoryNameList();
        model.addAttribute("categoryNameList", categoryNameList);
//        System.out.println(categoryNameList);
//        System.out.println("controller: categoryNameList" + categoryNameList);

//        List<Product> showProductList;
        CommonResponse commonResponse = productService.getProductList();
        model.addAttribute("showProductList", commonResponse.getData());
//        System.out.println(commonResponse.getData());
//        System.out.println("controller: showProductList" + commonResponse.getData());

        return "catalog/main";
    }

    /*用户注册*/
    @GetMapping("register")
    public String register(){
        return "user/registerBuyer";
    }

    @GetMapping("/bindPhoneNumber")
    public String returnFromGitee(@RequestParam("code") String code, ModelMap model){
        String giteeCustome = (String) model.getAttribute("giteeCustome");
        String giteeReason = (String) model.getAttribute("giteeReason");
        System.out.println(giteeCustome + " " + giteeReason);


        String userInfo = GiteeLogin.getUserInfo(code);
        System.out.println(userInfo);
        String giteeId = JSONObject.parseObject(userInfo).get("id").toString();
        System.out.println(giteeId);

        if(giteeCustome.equals("user")){
            CommonResponse commonResponse = userService.findUserByGiteeId(giteeId);
            if(giteeReason.equals("login")){    // 如果是用户登录使用gitee
                if(commonResponse.isSuccess()){
                    model.addAttribute("loginUser", commonResponse.getData());
                    /* todo:到首页 */
                    return "test_index";
                }else{
                    model.addAttribute("errorMsg", commonResponse.getMessage());
                    /* todo:到登录 */
                    return "test_index";
                }
            }

            if(giteeReason.equals("register")){
                if(commonResponse.isSuccess()){
                    model.addAttribute("errorMsg", "该gitee账号已被他人绑定。");
                    /* todo:到登录 */
                    return "test_index";
                }
                model.addAttribute("giteeId", giteeId);
//                todo:到bindphonumberandusername
                return "test_index";
            }
        }


//        String userInfo = GiteeLogin.getUserInfo(code);
        // 取出用户名
//        String name = (String) JSONObject.parseObject(userInfo).get("name");
        // 取出头像
//        String avatar_url = (String) JSONObject.parseObject(userInfo).get("avatar_url");

        System.out.println(userInfo);
        return "test_index";
    }
}
